package com.jniproject;

import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

/**
 * native方法名。不要带_下划线！！！！不要带_下划线！！！！不要带_下划线！！！！
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        Log.i(TAG, "onCreate: add===" + add(1, 2));
        int[] array = {1, 2, 3, 4};
        int[] newarrays = increaseArrayEles(array);
        for (int i = 0; i < newarrays.length; i++) {
            Log.i(TAG, "onCreate: increaseArrayEles===" + newarrays[i]);
        }
        Log.i(TAG, "onCreate: checkPwd===" + checkPwd("123456"));
        Log.i(TAG, "onCreate: checkPwd===" + checkPwd("123"));
        try {
            Log.i(TAG, "onCreate: sayHello===" + sayHello("----MM--- "));
        } catch (Exception e) {
            Log.i(TAG, "onCreate: NDKKKKKKKKKKKKK");
            e.printStackTrace();
        }

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    //1
    public native String stringFromJNI();

    //2
    public native int add(int x, int y);

    //3 字符串拼接失败
    public native String sayHello(String s);

    public native int[] increaseArrayEles(int[] intArray);

    public native boolean checkPwd(String pwd);

    //1 helloworld
    //btn-click->java->jni->java   传递数据（jni->java string）
    public void callBackJavaHelloWorld(View view) {
        jniToJava();
    }

    public void callBackJavaHelloWorld22(String str) {
        Log.i(TAG, "callBackJavaHelloWorld22: ====执行了" + str);
    }

    public void callBackJavaStatic(View view) {
        jniToJavaStatic();
    }

    public static void callBackJavaStatic22() {
        Log.i(TAG, "callBackJavaStatic22: ===执行了");
    }

    //btn-click->java->jni->java
    public void callBackJavaToast(View view) {
        jniToJavaToast();
    }

    public void callBackJavaToast22() {
        Toast.makeText(this, "Jni 调用 Java 成功了", Toast.LENGTH_SHORT).show();
    }

    public native void jniToJava();

    public native void jniToJavaStatic();

    public native void jniToJavaToast();

    public void getFloatArray(View view) {
        //JNI生成数组，并返回
        long path = 123L;
        float[] floats = nativeGetFloatArray(path);
        for (int i = 0; i < floats.length; i++) {
            Log.i(TAG, "getFloatArray: "+ floats[i]);
        }

        Log.i(TAG, "getFloatArray: --------");

        float[] data = {0.2F, 0.8F, 0.5F};
        //传递数组给JNI，然后返回
        int i = nativeGetFloatArray2(data);
        if (i == 1) {
            for (int j = 0; j < data.length; j++) {
                Log.i(TAG, "getFloatArray: " + data[j]);
            }
        }
    }

    //JNI生成数组，并返回
    //descriptor: ([F)I
    public native int nativeGetFloatArray2(float[] data);

    //传递数组给JNI，然后返回
    //descriptor: (J)[F
    public native float[] nativeGetFloatArray(long path);

    public void getIntArray(View view) {
        int[] ints = nativeGetIntArray();
        for (int i = 0; i < ints.length; i++) {
            Log.i(TAG, "getIntArray: "+ints[i]);
        }

        Log.i(TAG, "getIntArray: ------");

        int[] data = {1, 2, 3};
        data = nativeGetIntArray2(data);
        for (int j = 0; j < data.length; j++) {
            Log.i(TAG, "getIntArray: " + data[j]);
        }
    }

    //descriptor: ()[I
    public native int[] nativeGetIntArray();

    //descriptor: ([I)[I
    public native int[] nativeGetIntArray2(int[] data);

    Gson gson= new Gson();
    public void quest(View view) {
        Log.i(TAG, "quest: ");

        Uri.Builder builder = new Uri.Builder();
        final Uri buildUri = builder.scheme("content")
                .authority("com.facemasksdkdemo.MaskProvider")
                .build();
        final ContentValues cv = new ContentValues();
        MaskData maskData = new MaskData();
        maskData.path = 1234567890123L;
        maskData.imageWidth = 1;
        maskData.imageHeight =2;
        float[] floats = new float[50];
        for (int i = 0; i < floats.length; i++) {
            floats[i] = 100000.1234f+(float)i;
        }
        maskData.faceLandMarks = floats;
        cv.put("data",gson.toJson(maskData));
        new Thread(new Runnable() {
            @Override
            public void run() {
                Uri insert = getContentResolver().insert(buildUri,cv);
                Log.i(TAG, "quest: insert result::"+insert.toString()+"  "+"success".equals(insert.toString()));
            }
        }).start();


    }




    //调用成功。。。。。native方法名。不要带_下划线！！！！不要带_下划线！！！！不要带_下划线！！！！
    private LedController mLedController = new LedController();
    public void led_init(View view) {
        Log.i(TAG, "led_init: ");
        int i = mLedController.ledinit();
        Log.i(TAG, "led_init: result:"+i+":::解释0:success other: fail");

    }
    public void led_breat(View view) {
        Log.i(TAG, "led_breat: ");
        int i = mLedController.whiteBreathLedIsEnable(0);
        Log.i(TAG, "led_breat: result:"+i+":::解释：return_value: 0-success other-fail");

    }
    public void led_breat1(View view) {
        Log.i(TAG, "led_breat1: ");
        int i = mLedController.whiteBreathLedIsEnable(1);
        Log.i(TAG, "led_breat: result:"+i+":::解释：return_value: 0-success other-fail");

    }
    public void led_exit(View view) {
        Log.i(TAG, "led_exit: ");
        mLedController.ledExit();
    }

    private static class MaskData {
        long path;
        int imageWidth;
        int imageHeight;
        float[] faceLandMarks;
    }

}
